package com.mxpio.erp.plan.service.impl;

import com.mxpio.erp.common.enums.OrderEnums;
import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.jpa.model.Order;
import com.mxpio.erp.common.order.service.OrderLineService;
import com.mxpio.erp.common.order.service.OrderService;
import com.mxpio.erp.common.plan.entity.*;
import com.mxpio.erp.common.plan.service.MpsLineService;
import com.mxpio.erp.common.plan.service.OrganizationDemandService;
import com.mxpio.erp.common.vo.NoticeClassVo;
import com.mxpio.erp.common.workshop.entity.*;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import com.mxpioframework.security.util.SecurityUtils;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrganizationDemandServiceImpl extends BaseServiceImpl<OrganizationDemand> implements OrganizationDemandService {
    @Autowired
    private OrderLineService bizOrderLineService;

    @Autowired
    private OrderService bizOrderService;
    @Override
    @Transactional
    public Result<OrganizationDemand> updateBpmnStatus(OrganizationDemand organizationDemand, OrderStatusEnums.BpmnStatus auditing) {
        organizationDemand.setBpmnStatus(auditing.getStatusCode());
        if (auditing.getStatusCode().equals(OrderStatusEnums.BpmnStatus.AUDITED.getStatusCode())){//审核通过时，才写入审核人和时间
            organizationDemand.setReviewer(SecurityUtils.getLoginUsername());
            organizationDemand.setReviewTime(new Date());
        }
        this.update(organizationDemand);
        return Result.OK(organizationDemand);
    }

    @Override
    @Transactional
    public Result<? extends Order> clear(String bizNo) {
        OrganizationDemand po = this.getById(OrganizationDemand.class, bizNo);
        if(OrderStatusEnums.OrderStatus.FINISHED.getStatusCode().equals(po.getOrderStatus())){
            Criteria c = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
            List<OrganizationDemandLine> lines = JpaUtil.linq(OrganizationDemandLine.class).where(c).list();

            if (lines.size()>0){
                //计算数量字段
                NoticeClassVo oDReceivingNoticeClazz = new NoticeClassVo(ODReceivingNotice.class, ODReceivingNoticeLine.class);
                NoticeClassVo oDRejectNotice = new NoticeClassVo(ODRejectNotice.class, ODRejectNoticeLine.class);
                NoticeClassVo oDSupplementNotice = new NoticeClassVo(ODSupplementNotice.class, ODSupplementNoticeLine.class);
                NoticeClassVo oDDefectiveRejNotice = new NoticeClassVo(ODDefectiveRejNotice.class, ODDefectiveRejNoticeLine.class);
                List<NoticeClassVo> noticeClassVos = new ArrayList<>();
                noticeClassVos.add(oDReceivingNoticeClazz);
                noticeClassVos.add(oDRejectNotice);
                noticeClassVos.add(oDSupplementNotice);//退料时考虑补料部分
                noticeClassVos.add(oDDefectiveRejNotice);
                bizOrderLineService.handleBizOrderLineQuantity(lines, noticeClassVos, OrderEnums.AccessType.OUT.getCode());

                for (OrganizationDemandLine orderItem:lines){
                    if ((orderItem.getPlanQuantity()!=null &&orderItem.getPlanQuantity().compareTo(BigDecimal.ZERO)!=0)
                            || (orderItem.getPlanRejectQuantity()!=null && orderItem.getPlanRejectQuantity().compareTo(BigDecimal.ZERO)!=0)){
                        return Result.error("订单行存在未执行完毕的领、退、补料记录，无法结算订单。");
                    }
                }
            }
            return bizOrderService.updateOrderStatus(po, OrderStatusEnums.OrderStatus.CLEARED);
        }else{
            return Result.error("非已完成状态不可执行”结算结清“操作");
        }
    }
}
